1
1
.
.
3
3
.
.
1
1
S
S
t
t
a
a
r
r
t
t
u
u
p
p
I
I
n
n
f
f
o
o
[
[
G
G
]
]
Following tutorials show how to create Class that implements CommandLineRunner to run Task at App startup.
You simply need to @Override its run() Method with your custom code.
You can use @Order(1) to specify order in which such Classes (and their run() Methods) should be executed.
Application Schema [Results]
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_commandlinerunner (Spring Boot Starters are not used)
Create Package: runners (inside main package)
– Create Class: MyRunner1.java (inside runners package)
– Create Class: MyRunner2.java (inside runners package)
MyRunner1.java
package com.ivoronline.springboot_commandlinerunner.runners;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(1)
@Component
public class MyRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Hello from MyRunner1");
}
}
MyRunner2.java
package com.ivoronline.springboot_commandlinerunner.runners;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(2)
@Component
public class MyRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Hello from MyRunner2");
}
}
MyRunner2
MyRunner1
@Order(1)
@Order(2)
R
R
e
e
s
s
u
u
l
l
t
t
s
s
Console
Hello from MyRunner1
Hello from MyRunner2
Application Structure